package com.jsg.sl651.message.parser.impl;

import com.jsg.sl651.constant.EncodingElementIdentifier;
import com.jsg.sl651.entity.data.Element;
import com.jsg.sl651.enums.CommunicationMessageType;
import com.jsg.sl651.message.parser.Parser;
import com.jsg.sl651.message.parser.model.StationStatus;

/**
 * 遥测站报警及状态信息
 */
public class StationStatusParser implements Parser<StationStatus> {
    /**
     * 解析Hex模式下的数据
     * <p>方法核心</p>
     * <ul>
     *     <li>解析该字节数组</li>
     *     <li>数据生成相应的数据对象{@link CommunicationMessageType}</li>
     * </ul>
     *
     * @param element 数据类型定义 {@link EncodingElementIdentifier.EncodingElement}
     * @param bytes   字节数组
     * @return 解析出来的数据对象
     */
    @Override
    public Element<StationStatus> parserHexImpl(EncodingElementIdentifier.EncodingElement element, byte[] bytes) throws Exception {
        StationStatus stationStatus = new StationStatus(bytes);
        return new Element<>(bytes, stationStatus, element);
    }

    /**
     * 解析Hex模式下的数据
     * <p>方法核心</p>
     * <ul>
     *     <li>解析该字节数组</li>
     *     <li>数据生成相应的数据对象{@link CommunicationMessageType}</li>
     * </ul>
     *
     * @param element 数据类型定义 {@link EncodingElementIdentifier.EncodingElement}
     * @param bytes   字节数组
     * @return 解析出来的数据对象
     */
    @Override
    public Element<StationStatus> parserAsciiImpl(EncodingElementIdentifier.EncodingElement element, byte[] bytes) throws Exception {
        if (bytes.length == 32) {
            String str = bytesToString(bytes);
            bytes = new byte[4];
            for (int i = 0; i < 4; i++) {
                String substring = str.substring(i * 8, i * 8 + 8);
                int i1 = Integer.parseInt(substring, 2);
                bytes[i] = (byte) i1;
            }
        }
        StationStatus stationStatus = new StationStatus(bytes);
        return new Element<>(bytes, stationStatus, element);
    }
}
